import 'package:flutter_admin/api/odoo/product_workreportrecord_odoo_api.dart';
import 'package:flutter_admin/models/odoo/udt_tb_workreportrecord.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/utils/utils.dart';

class ProductWorkreportList extends StatefulWidget {
  UdtTbWorkreportrecord? udtTbWorkreportrecord;
  ProductWorkreportList({required this.udtTbWorkreportrecord, Key? key})
      : super(key: key);

  @override
  _ProductWorkreportListState createState() => _ProductWorkreportListState();
}

class _ProductWorkreportListState extends State<ProductWorkreportList> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  late UdtTbWorkreportrecord udtTbWorkreportrecord;
  UdtTbWorkreportrecord udtTbWorkreport = UdtTbWorkreportrecord();
  List tableList = [];
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _loadData();
    });
  }

  @override
  Widget build(BuildContext context) {
    var _columns = (widget.udtTbWorkreportrecord!.teamCode == 'PTPFA'
        ||widget.udtTbWorkreportrecord!.teamCode == 'PTPFB'
        ||widget.udtTbWorkreportrecord!.teamCode == 'PTPFC')?[
      DataColumn(label: Text('生产单号')),
      DataColumn(label: Text("产品图号")),
      DataColumn(label: Text("产品名称")),
      DataColumn(label: Text("材料型号")),
      DataColumn(label: Text("设备工位")),
      DataColumn(label: Text("生产工序")),
      DataColumn(label: Text("喷涂面积")),
      DataColumn(label: Text("合格数量")),
      DataColumn(label: Text("不合格数量")),
      DataColumn(label: Text("备货数量")),
      DataColumn(label: Text("换模个数")),
      DataColumn(label: Text("刀数")),
      DataColumn(label: Text("计划数量")),
      DataColumn(label: Text("报工人")),
      DataColumn(label: Text("报工日期")),
      DataColumn(label: Text("生产类型")),
      DataColumn(label: Text("备注类型")),
      DataColumn(label: Text("备注")),
    ]:[
      DataColumn(label: Text('生产单号')),
      DataColumn(label: Text("产品图号")),
      DataColumn(label: Text("产品名称")),
      DataColumn(label: Text("材料型号")),
      DataColumn(label: Text("设备工位")),
      DataColumn(label: Text("生产工序")),
      DataColumn(label: Text("合格数量")),
      DataColumn(label: Text("不合格数量")),
      DataColumn(label: Text("备货数量")),
      DataColumn(label: Text("换模个数")),
      DataColumn(label: Text("刀数")),
      DataColumn(label: Text("计划数量")),
      DataColumn(label: Text("报工人")),
      DataColumn(label: Text("报工日期")),
      DataColumn(label: Text("生产类型")),
      DataColumn(label: Text("备注类型")),
      DataColumn(label: Text("备注")),
    ];
    var table = DataTable(
      columns: _columns,
      rows: this.tableList.map((m) {
        UdtTbWorkreportrecord udtTbWorkreport =
            UdtTbWorkreportrecord.fromDetailMap(m);

        var result = DataRow(cells: (widget.udtTbWorkreportrecord!.teamCode == 'PTPFA'
            ||widget.udtTbWorkreportrecord!.teamCode == 'PTPFB'
            ||widget.udtTbWorkreportrecord!.teamCode == 'PTPFC')?[
          DataCell(Text(udtTbWorkreport.productionorderno ?? '--')),
          DataCell(Text(udtTbWorkreport.productCode ?? '--')),
          DataCell(Text(udtTbWorkreport.productName ?? '--')),
          DataCell(Text(udtTbWorkreport.spec ?? '--')),
          DataCell(Text(udtTbWorkreport.stationName ?? '--')),
          DataCell(Text(udtTbWorkreport.processName ?? '--')),
          DataCell(Text('${num.parse(udtTbWorkreport.sprayArea!)*udtTbWorkreport.qualifiedquantity!}')),
          DataCell(Text(udtTbWorkreport.qualifiedquantity != null
              ? udtTbWorkreport.qualifiedquantity.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.unqualifiedquantity != null
              ? udtTbWorkreport.unqualifiedquantity.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.repairquantity != null
              ? udtTbWorkreport.repairquantity.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.replacenumber != null
              ? udtTbWorkreport.replacenumber.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.cuttingD ?? '--')),
          DataCell(Text(udtTbWorkreport.productQty ?? '--')),
          DataCell(Text(udtTbWorkreport.reportworkerName ?? '--')),
          DataCell(Text(udtTbWorkreport.reportDate ?? '--')),
          DataCell(Text(udtTbWorkreport.productionType ?? '--')),
          DataCell(Text(udtTbWorkreport.remarkCate ?? '--')),
          DataCell(Text(udtTbWorkreport.remark ?? '--')),
        ]:[
          DataCell(Text(udtTbWorkreport.productionorderno ?? '--')),
          DataCell(Text(udtTbWorkreport.productCode ?? '--')),
          DataCell(Text(udtTbWorkreport.productName ?? '--')),
          DataCell(Text(udtTbWorkreport.spec ?? '--')),
          DataCell(Text(udtTbWorkreport.stationName ?? '--')),
          DataCell(Text(udtTbWorkreport.processName ?? '--')),
          DataCell(Text(udtTbWorkreport.qualifiedquantity != null
              ? udtTbWorkreport.qualifiedquantity.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.unqualifiedquantity != null
              ? udtTbWorkreport.unqualifiedquantity.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.repairquantity != null
              ? udtTbWorkreport.repairquantity.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.replacenumber != null
              ? udtTbWorkreport.replacenumber.toString()
              : '--')),
          DataCell(Text(udtTbWorkreport.cuttingD ?? '--')),
          DataCell(Text(udtTbWorkreport.productQty ?? '--')),
          DataCell(Text(udtTbWorkreport.reportworkerName ?? '--')),
          DataCell(Text(udtTbWorkreport.reportDate ?? '--')),
          DataCell(Text(udtTbWorkreport.productionType ?? '--')),
          DataCell(Text(udtTbWorkreport.remarkCate ?? '--')),
          DataCell(Text(udtTbWorkreport.remark ?? '--')),
        ]);

        return result;
      }).toList(),
    );
    CryButtonBar buttonBar = CryButtonBar(
      children: <Widget>[
        CryButton(
          label: "导出",
          onPressed: () => _generateExcel(),
          iconData: Icons.import_export_outlined,
        )
      ],
    );
    var result = Scaffold(
        key: scaffoldStateKey,
        appBar: AppBar(
          leading: Builder(
            builder: (BuildContext context) {
              final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
              final bool canPop = parentRoute?.canPop ?? false;

              if (canPop) {
                return BackButton();
              } else {
                return SizedBox.shrink();
              }
            },
          ),
          title: Text('报工人明细信息'),
        ),
        body: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(height: 10),
              buttonBar,
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: tableList.length != 0 ? table : null,
              ),
            ],
          ),
        ));
    return result;
  }

  Future<dynamic> findAll() async {
    formKey.currentState?.save();
    List<UdtTbWorkreportrecord> resultList = [];
    ResponseBodyApi responseBodyApi =
        await ProductWorkreportrecordOdooApi.findDetailAll(data: {
      "domainList": [
        if (widget.udtTbWorkreportrecord!.reportworkerId != null &&
            widget.udtTbWorkreportrecord!.reportworkerId != '')
          [
            'reportworker_id',
            '=',
            int.parse(widget.udtTbWorkreportrecord!.reportworkerId!)
          ],
        if (widget.udtTbWorkreportrecord!.reportDate != null &&
            widget.udtTbWorkreportrecord!.reportDate != '')
          ['date', '=', widget.udtTbWorkreportrecord!.reportDate],
        if (widget.udtTbWorkreportrecord!.workshopId != null &&
            widget.udtTbWorkreportrecord!.workshopId != '')
          [
            'workshop_id',
            '=',
            int.parse(widget.udtTbWorkreportrecord!.workshopId!)
          ],
        if (widget.udtTbWorkreportrecord!.teamId != null &&
            widget.udtTbWorkreportrecord!.teamId != '')
          ['team_id', '=', int.parse(widget.udtTbWorkreportrecord!.teamId!)],
      ],
    });
    if (responseBodyApi.success!) {
      resultList = responseBodyApi.data
              .map<UdtTbWorkreportrecord>(
                  (e) => UdtTbWorkreportrecord.fromDetailMap(e))
              .toList() ??
          [];
    }
    return resultList;
  }

  _generateExcel() async {
    List<UdtTbWorkreportrecord> selectedList = await findAll();
    if (selectedList == null) {
      return;
    }
    List<Map> map = (widget.udtTbWorkreportrecord!.teamCode == 'PTPFA'
        ||widget.udtTbWorkreportrecord!.teamCode == 'PTPFB'
        ||widget.udtTbWorkreportrecord!.teamCode == 'PTPFC')?[
      {
        'name': 'productionorderno',
        'label': '生产单号',
        'columnWidth': 20.0,
      },
      {
        'name': 'productCode',
        'label': '产品图号',
        'columnWidth': 16.0,
      },
      {
        'name': 'productName',
        'label': '产品名称',
        'columnWidth': 19.0,
      },
      {
        'name': 'spec',
        'label': '材料型号',
        'columnWidth': 26.0,
      },
      {
        'name': 'stationName',
        'label': '设备工位',
        'columnWidth': 13.0,
      },
      {
        'name': 'processName',
        'label': '生产工序',
        'columnWidth': 13.0,
      },
      {
        'name': 'sprayArea',
        'label': '喷涂面积',
        'columnWidth': 13.0,
      },
      {
        'name': 'qualifiedquantity',
        'label': '合格数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'unqualifiedquantity',
        'label': '不合格数量',
        'columnWidth': 15.0,
      },
      {
        'name': 'repairquantity',
        'label': '备货数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'replacenumber',
        'label': '换模数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'cuttingD',
        'label': '刀数',
        'columnWidth': 8.0,
      },
      {
        'name': 'productQty',
        'label': '计划数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'reportworkerName',
        'label': '报工人',
        'columnWidth': 10.0,
      },
      {
        'name': 'reportDate',
        'label': '报工日期',
        'columnWidth': 22.0,
      },
      {
        'name': 'productionType',
        'label': '生产类型',
        'columnWidth': 22.0,
      },
      {
        'name': 'remarkCate',
        'label': '备注类型',
        'columnWidth': 22.0,
      },
      {
        'name': 'remark',
        'label': '备注"',
        'columnWidth': 22.0,
      },
    ]:[
      {
        'name': 'productionorderno',
        'label': '生产单号',
        'columnWidth': 20.0,
      },
      {
        'name': 'productCode',
        'label': '产品图号',
        'columnWidth': 16.0,
      },
      {
        'name': 'productName',
        'label': '产品名称',
        'columnWidth': 19.0,
      },
      {
        'name': 'spec',
        'label': '材料型号',
        'columnWidth': 26.0,
      },
      {
        'name': 'stationName',
        'label': '设备工位',
        'columnWidth': 13.0,
      },
      {
        'name': 'processName',
        'label': '生产工序',
        'columnWidth': 13.0,
      },
      {
        'name': 'qualifiedquantity',
        'label': '合格数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'unqualifiedquantity',
        'label': '不合格数量',
        'columnWidth': 15.0,
      },
      {
        'name': 'repairquantity',
        'label': '备货数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'replacenumber',
        'label': '换模数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'cuttingD',
        'label': '刀数',
        'columnWidth': 8.0,
      },
      {
        'name': 'productQty',
        'label': '计划数量',
        'columnWidth': 13.0,
      },
      {
        'name': 'reportworkerName',
        'label': '报工人',
        'columnWidth': 10.0,
      },
      {
        'name': 'reportDate',
        'label': '报工日期',
        'columnWidth': 22.0,
      },
      {
        'name': 'productionType',
        'label': '生产类型',
        'columnWidth': 22.0,
      },
      {
        'name': 'remarkCate',
        'label': '备注类型',
        'columnWidth': 22.0,
      },
      {
        'name': 'remark',
        'label': '备注"',
        'columnWidth': 22.0,
      },
    ];
    Utils.generateExcel(
        fileName: "车间人员明细数据", configure: map, data: selectedList);
  }

  _loadData() async {
    ResponseBodyApi responseBodyApi =
        await ProductWorkreportrecordOdooApi.data(data: {
      "domainList": [
        if (widget.udtTbWorkreportrecord!.reportworkerId != null &&
            widget.udtTbWorkreportrecord!.reportworkerId != '')
          [
            'reportworker_id',
            '=',
            int.parse(widget.udtTbWorkreportrecord!.reportworkerId!)
          ],
        if (widget.udtTbWorkreportrecord!.reportDate != null &&
            widget.udtTbWorkreportrecord!.reportDate != '')
          ['date', '=', widget.udtTbWorkreportrecord!.reportDate],
        if (widget.udtTbWorkreportrecord!.workshopId != null &&
            widget.udtTbWorkreportrecord!.workshopId != '')
          [
            'workshop_id',
            '=',
            int.parse(widget.udtTbWorkreportrecord!.workshopId!)
          ],
        if (widget.udtTbWorkreportrecord!.teamId != null &&
            widget.udtTbWorkreportrecord!.teamId != '')
          ['team_id', '=', int.parse(widget.udtTbWorkreportrecord!.teamId!)],
      ],
    });
    if (responseBodyApi.success!) {
      tableList = responseBodyApi.data;
      setState(() {});
    }
  }
}
